package com.dy.drp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.drp.dto.BuyinfoDto;
import com.dy.drp.entity.*;
import com.dy.drp.mapper.*;
import com.dy.drp.service.IBuyinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.drp.service.IBuyshorttimeService;
import com.dy.drp.service.IProviderService;
import com.dy.drp.util.PageUtil;
import com.dy.drp.util.SelectUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dy
 * @since 2022-04-02
 */
@Service
public class BuyinfoServiceImpl extends ServiceImpl<BuyinfoMapper, Buyinfo> implements IBuyinfoService {
    @Autowired
    private ProviderMapper providerMapper;
    @Autowired
    private BuyinfoMapper buyinfoMapper;
    @Autowired
    private BuyshorttimeMapper buyshorttimeMapper;
    @Autowired
    private BuylistMapper buylistMapper;
    @Autowired
    private CargoMapper cargoMapper;

    @Override
    @Transactional
    public boolean insertIntoFactory(String providerName, String buyPerson) {
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.eq("provider_name",providerName);
        Provider one = providerMapper.selectOne(wrapper);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String time = df.format(new Date());
        Buyinfo buyinfo = new Buyinfo();
        buyinfo.setProviderId(one.getProviderId());
        buyinfo.setBuyTime(time);
        buyinfo.setBuyPerson(buyPerson);
        buyinfoMapper.insertOneBuyInfo(buyinfo);
        List<Buyshorttime> buyshorttimes = buyshorttimeMapper.selectList(null);
        buyshorttimes.stream().forEach(r->{
            Buylist buylist = new Buylist(null,r.getCargoId(),buyinfo.getBuyId(),r.getShortNumber());
            buylistMapper.insert(buylist);
            QueryWrapper<Cargo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("cargo_id", r.getCargoId());
            Cargo cargo = cargoMapper.selectOne(wrapper1);
            cargo.setCargoNumber(cargo.getCargoNumber()+r.getShortNumber());
            cargoMapper.update(cargo, wrapper1);
        });
        Integer truncate = buyshorttimeMapper.truncate();
        if (truncate == 0){
            return true;
        }
        return false;
    }

    @Override
    public PageUtil<BuyinfoDto> selectOnePageBuyinfo(PageUtil<BuyinfoDto> pageUtil) {
        Integer integer = buyinfoMapper.selectCount(null);
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);
        List<BuyinfoDto> buyinfoDtos = buyinfoMapper.selectOnePageBuyinfo(pageUtil);
        pageUtil.setList(buyinfoDtos);
        return pageUtil;
    }

    @Override
    public PageUtil<BuyinfoDto> selectOnePageBuyinfoByInfo(SelectUtil selectUtil) {
        PageUtil<BuyinfoDto> pageUtil = new PageUtil<>();
        pageUtil.setPageSize(selectUtil.getPageSize());
        pageUtil.setPageNum(selectUtil.getPageNum());

        QueryWrapper<Buyinfo> wrapper = new QueryWrapper<>();
        wrapper.inSql("provider_id","select provider_id from t_provider where provider_name like '%"+selectUtil.getName()+"%'");
        wrapper.between("buy_time",selectUtil.getStartTime(),selectUtil.getEndTime());
        Integer integer = buyinfoMapper.selectCount(wrapper);
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);

        List<BuyinfoDto> buyinfoDtos = buyinfoMapper.selectOnePageBuyinfoByInfo(selectUtil);
        pageUtil.setList(buyinfoDtos);
        System.out.println(pageUtil);
        return pageUtil;
    }

    @Override
    public BuyinfoDto selectOneBuyById(Long buyId) {
        return buyinfoMapper.selectOneBuyById(buyId);
    }
}
